# coding=utf-8
from django.db import models


class SimpleBaseMode(models.Model):
    is_valid = models.BooleanField(default=True, verbose_name=u'有效性')
    is_deleted = models.BooleanField(default=False, verbose_name=u'是否被删除')

    def valid_action(self, valid=True):
        self.is_value = valid
        self.save()

    class Meta:
        abstract = True
        db_table = 'simple_base_mode'
        app_label = 'auto_sale'




class BaseModel(SimpleBaseMode):
    created_time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建时间')
    modified_time = models.DateTimeField(auto_now=True, verbose_name=u'修改时间')

    class Meta:
        abstract = True
        db_table = 'base_model'
        app_label = 'auto_sale'

class Shop(BaseModel):
    name = models.CharField(u'商店名称', max_length=50)
    url = models.CharField(u'商店网址', max_length=100)

    spider_shop_id = models.IntegerField(u'spider中shop的id')

    class Meta:
        db_table = 'shop'
        app_label = 'auto_sale'

    def __unicode__(self):
        return 'id=' + str(self.id) + ' name=' + self.name


# 产品类
class Product(BaseModel):
    name = models.CharField(u'产品名称', max_length=50)
    url = models.CharField(u'产品网站', max_length=100)
    price = models.FloatField(u'商品价格', default=9999)
    count = models.IntegerField(u'商品总库存', default=0)
    image = models.CharField(u'商品在列表中的图片', max_length=100)
    is_on_sale = models.BooleanField(u'是否出售', default=True)
    spider_product_id = models.IntegerField(u'商品对应的spider中product的id')

    shop = models.ForeignKey(Shop)

    class Meta:
        db_table = 'product'
        app_label = 'auto_sale'

    def __unicode__(self):
        return 'id=' + str(self.id).decode('utf-8') + ' name=' + self.name.decode('utf-8')


# 尺寸类
class Size(SimpleBaseMode):
    name = models.CharField(u'尺寸名称', max_length=50)
    product = models.ForeignKey(Product)
    # 由于编辑界面是和spider中的models对应的
    # 这里的models只是spider中的models的一个镜像
    spider_size_id = models.IntegerField(u'spider中size的id')

    class Meta:
        db_table = 'size'
        app_label = 'auto_sale'

    def __unicode__(self):
        return 'id=' + str(self.id) + ' name=' + self.name


# 颜色类
class Color(SimpleBaseMode):
    name = models.CharField(u'颜色名称', max_length=50)
    product = models.ForeignKey(Product)

    spider_color_id = models.IntegerField(u'spider中color的id')

    class Meta:
        db_table = 'color'
        app_label = 'auto_sale'

    def __unicode__(self):
        return 'color_id=' + str(self.id) + ' name=' + self.name


# 库存类
class Inventory(SimpleBaseMode):
    count = models.IntegerField(u'每个sku的库存数量', default=0)
    product = models.ForeignKey(Product)
    size = models.ForeignKey(Size)
    color = models.ForeignKey(Color)

    spider_inventory_id = models.IntegerField(u'spider中inventory的id')

    class Meta:
        db_table = 'inventory'
        app_label = 'auto_sale'

    def __unicode__(self):
        return 'id=' + str(self.id) + ' count=' + str(self.count) + ' product_id=' + str(self.product.id)


# 顶部图片类
class BoothImage(SimpleBaseMode):
    product = models.ForeignKey(Product)
    local_path = models.CharField(u'图片路径', max_length=50)
    net_path = models.CharField(u'图片网络路径', max_length=100)

    spider_booth_image_id = models.IntegerField(u'spider中BoothImage的id')

    class Meta:
        db_table = 'booth_image'
        app_label = 'auto_sale'

    def __unicode__(self):
        return 'id=' + str(self.id) + ' net_path=' + self.net_path + ' product_id=' + str(
            self.product.id)


# 底部图片类
class DetailImage(SimpleBaseMode):
    product = models.ForeignKey(Product)
    local_path = models.CharField(u'图片路径', max_length=50)
    net_path = models.CharField(u'图片网络路径', max_length=100)

    spider_detail_image_id = models.IntegerField(u'spider中DetailImage的id')

    class Meta:
        db_table = 'detail_image'
        app_label = 'auto_sale'

    def __unicode__(self):
        return 'id=' + str(self.id) + ' net_path=' + self.net_path + ' product=' + str(
            self.product.id)
